//
//  BoardView.swift
//  DiceGame
//
//  Created by tanwz on 16/11/17.
//  Copyright © 2016年 tanwz. All rights reserved.
//

import UIKit

class BoardView: UIView {
    var size: Int = 5 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    var ladders: [(Int, Int)]?
    var snakes: [(Int, Int)]?
    
    private(set) var boards:[CGRect]?
    private let margin: CGFloat = 2.0
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func layoutSubviews() {
        initBoards()
    }
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        
        //draw line
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.saveGState()
        
        ctx?.setFillColor(UIColor.white.cgColor)
        ctx?.fill(rect)
        
        ctx?.setStrokeColor(UIColor.lightGray.cgColor)
        
        let boardWidth = (frame.size.width - margin * 2) / CGFloat(size);
        for i in 0...size {
            // horizon
            ctx?.move(to: CGPoint(x: margin, y: CGFloat(i) * boardWidth + margin))
            ctx?.addLine(to: CGPoint(x: frame.size.width - margin, y: CGFloat(i) * boardWidth + margin))
            
            //verticle
            ctx?.move(to: CGPoint(x: CGFloat(i) * boardWidth + margin, y: margin))
            ctx?.addLine(to: CGPoint(x: CGFloat(i) * boardWidth + margin, y: frame.size.height - margin))
        }
        ctx?.strokePath()
        
        let font = UIFont.systemFont(ofSize: 17)
        let attr = [NSFontAttributeName:font, NSForegroundColorAttributeName:UIColor.black]
        for i in 0..<(size * size) {
            NSString(format: "%d", i + 1).draw(in: boards![i], withAttributes:attr)
        }
        
        //draw ladders
        if let ladders = ladders {
            ctx?.setStrokeColor(UIColor.green.cgColor)
            ctx?.setLineWidth(4)
            for i in 0..<ladders.count {
                let (from, to) = ladders[i]
                ctx?.move(to: boardCenter(at: from))
                ctx?.addLine(to: boardCenter(at: to))
                ctx?.addArc(center: boardCenter(at: to), radius: 4, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: false)
            }
            ctx?.strokePath()
        }
        
        //draw snakes
        if let snakes = snakes {
            ctx?.setStrokeColor(UIColor.red.cgColor)
            ctx?.setLineWidth(4)
            for i in 0..<snakes.count {
                let (from, to) = snakes[i]
                ctx?.move(to: boardCenter(at: from))
                ctx?.addLine(to: boardCenter(at: to))
                ctx?.addArc(center: boardCenter(at: to), radius: 4, startAngle: 0, endAngle: CGFloat(M_PI * 2), clockwise: false)
            }
            ctx?.strokePath()
        }
        
        ctx?.restoreGState()
    }
    
    func boardCenter(at index: Int) -> CGPoint {
        return CGPoint(x: boards![index].midX, y: boards![index].midY)
    }
    
    //MARK: - prv
    func initBoards() {
        let boardWidth = (frame.size.width - margin * 2) / CGFloat(size);
        
        if boards == nil {
            boards = [CGRect]()
        }
        boards?.removeAll()
        
        for i in 0..<(size * size) {
            
            var x = CGFloat(i % size) * boardWidth + margin
            if (i / size) % 2 != 0 {
                x = CGFloat(size - 1 - i % size) * boardWidth + margin
            }
            
            let origin = CGPoint(x: x,
                                 y: CGFloat(size - 1 - i / size) * boardWidth + margin)
            boards?.append(CGRect(origin: origin, size: CGSize(width: boardWidth, height: boardWidth)))
        }
    }
    
    
}
